{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "from PIL import Image\n",
    "import os\n",
    "import numpy as np\n",
    "from vdec_process.acl_vdec import Vdec\n",
    "import matplotlib.pyplot as plt\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# video_path = \"/home/a800/test_data/dog60s.h264\"\n",
    "\n",
    "video_path = \"/home/HwHiAiUser/shot-analyzer/video/ckc4k22t3yx0y07183vok7h89.mov\"\n",
    "# input_height, input_width= 1440, 2560\n",
    "# input_height, input_width= 1080, 1920\n",
    "# input_height, input_width = 684, 1024\n",
    "cap = cv2.VideoCapture(video_path)\n",
    "# cap.set(cv2.CAP_PROP_FORMAT, -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "0.5809016227722168\n",
      "3.898668609209509\n"
     ]
    }
   ],
   "source": [
    "t1 = time.time()\n",
    "video_path = \"/home/HwHiAiUser/shot-analyzer/video/ckc4k22t3yx0y07183vok7h89.mov\"\n",
    "# input_height, input_width= 1440, 2560\n",
    "input_height, input_width= 1080, 1920\n",
    "# input_height, input_width = 684, 1024\n",
    "cap = cv2.VideoCapture(video_path)\n",
    "frames = list()\n",
    "cnt = 0\n",
    "print(cap.isOpened())\n",
    "while cap.isOpened():\n",
    "    \n",
    "    ret, frame = cap.read()\n",
    "    if ret:\n",
    "        cnt += 1\n",
    "        frames.append(frame)\n",
    "        continue\n",
    "    else:\n",
    "        break\n",
    "        \n",
    "t2 = time.time()\n",
    "print(t2-t1)\n",
    "print((t2-t1) * 1000 / cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %timeit -n 100 -r 10 \n",
    "vdec_process = Vdec(input_width, input_height)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# %timeit \n",
    "t1 = time.time()\n",
    "vdec_process.run(video_path)\n",
    "t2 = time.time()\n",
    "print(t2-t1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# Fetch YUV format images\n",
    "images_buffer = vdec_process.get_image_buffer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "len(images_buffer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# YUV to BGR\n",
    "\n",
    "t1 = time.time()\n",
    "\n",
    "for idx in range(len(images_buffer)):\n",
    "#     rgb_imgs[idx] = \n",
    "    cv2.cvtColor(images_buffer[idx].reshape(-1, input_width), cv2.COLOR_YUV2BGR_NV12)\n",
    "#     print(a.shape)\n",
    "#     if idx % 10 == 0:\n",
    "#         print(\"Processed %d images\" %idx)\n",
    "# rgb_imgs = [cv2.cvtColor(i.reshape(-1, input_width), cv2.COLOR_YUV2BGR_NV12) for i in images_buffer]\n",
    "# images_buffer\n",
    "# cv2.cvtColor(images_buffer[0].reshape(-1, input_width), cv2.COLOR_YUV2BGR_NV12)\n",
    "t2 = time.time()\n",
    "print(t2-t1)\n",
    "print((t2-t1)/len(images_buffer))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
